package com.customization.unionpay.market.util;

import com.alibaba.fastjson.JSONObject;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.List;
import java.util.UUID;

/**
 * @author sjm
 * @description 云闪付相关请求
 */
public class CloudFlashPaymentUtils {
    private static final Logger log = LoggerFactory.getLogger(CloudFlashPaymentUtils.class);

    /**
     * 预算授权码生成（新增+审核通过）
     * @param datas
     * @return
     * @throws Exception
     */
    public static String innerGenerateByBudgetCode(JSONObject datas) throws Exception {
        // 服务名称
        String serviceId = PropUtils.BB_LOG.getPropValue("marketSubject", "cloudFlashPaymentServiceId");
        // api名称
        String api = PropUtils.BB_LOG.getPropValue("marketSubject", "innerGenerateBudgetCode");
        // 接入方ID
        String openid = PropUtils.BB_LOG.getPropValue("marketSubject", "openid");
        // 用户ID
        String userId = PropUtils.BB_LOG.getPropValue("marketSubject", "userId");
        // 机构ID
        String orgId = PropUtils.BB_LOG.getPropValue("marketSubject", "orgId");
        // 随机流水号
        String msgId = UUID.randomUUID().toString();

        JSONObject params = new JSONObject();
        params.put("openid", openid);
        params.put("userId", userId);
        params.put("orgId", orgId);
        params.put("api", api);
        params.put("msgId", msgId);

        params.put("data", datas);

        log.info("请求api：" + api + ",请求参数：" + params);

        return MagpieUtils.getCloudFlashPayment(params,serviceId);
    }

    /**
     * 预算授权码调整（调整+调整审核通过）
     * @param datas
     * @return
     * @throws Exception
     */
    public static String innerAdjustByBudgetCode(JSONObject datas) throws Exception {

        // 服务名称
        String serviceId = PropUtils.BB_LOG.getPropValue("marketSubject", "cloudFlashPaymentServiceId");
        // api名称
        String api = PropUtils.BB_LOG.getPropValue("marketSubject", "innerAdjustBudgetCode");
        // 接入方ID
        String openid = PropUtils.BB_LOG.getPropValue("marketSubject", "openid");
        // 用户ID
        String userId = PropUtils.BB_LOG.getPropValue("marketSubject", "userId");
        // 机构ID
        String orgId = PropUtils.BB_LOG.getPropValue("marketSubject", "orgId");
        // 随机流水号
        String msgId = UUID.randomUUID().toString();

        JSONObject params = new JSONObject();
        params.put("openid", openid);
        params.put("userId", userId);
        params.put("orgId", orgId);
        params.put("api", api);
        params.put("msgId", msgId);

        params.put("data", datas);

        log.info("请求api：" + api + ",请求参数：" + params);

        return MagpieUtils.getCloudFlashPayment(params,serviceId);
    }


    /**
     * 批量查询授权码
     * @param list
     * @return
     * @throws Exception
     */
    public static String batchQueryByBudgetAuthCode(List<String> list) throws Exception {
        // 服务名称
        String serviceId = PropUtils.BB_LOG.getPropValue("marketSubject", "cloudFlashPaymentServiceId");
        // api名称
        String api = PropUtils.BB_LOG.getPropValue("marketSubject", "batchQueryBudgetAuthCodeUrl");
        // 接入方ID
        String openid = PropUtils.BB_LOG.getPropValue("marketSubject", "openid");
        // 用户ID
        String userId = PropUtils.BB_LOG.getPropValue("marketSubject", "userId");
        // 机构ID
        String orgId = PropUtils.BB_LOG.getPropValue("marketSubject", "orgId");
        // 随机流水号
        String msgId = UUID.randomUUID().toString();

        JSONObject params = new JSONObject();
        params.put("openid", openid);
        params.put("userId", userId);
        params.put("orgId", orgId);
        params.put("api", api);
        params.put("msgId", msgId);
        JSONObject datas = new JSONObject();
        datas.put("budgetAuthCodeList", list);
        params.put("data", datas);

        log.info("请求api：" + api + ",请求参数：" + params);

        return MagpieUtils.getCloudFlashPayment(params,serviceId);
    }
}

